// const BASE_URL = '192.168.0.203'//本地局域网
var BASE_URL = getApp().globalData.BASE_URL
var platform = 'app'
const isAgency = getApp().globalData.isAgency

export const request = function(url, data, method = "GET", showLoading = true, header = {
	// 'Content-Type': 'application/x-www-form-urlencoded'
}, ) {
	const sessionData = uni.getStorageSync('sessionData')
	// #ifdef H5
	platform = 'h5'
	if (isAgency) {
		BASE_URL = '/api'
	}
	// #endif
	let sessionDataLink = "";
	if(sessionData && sessionData.session_id){
		sessionDataLink = `&session_id=${sessionData.session_id}`;
	}
	const link = `${BASE_URL}?${url}&platform=${platform}&aid=1${sessionDataLink}`
	var promise = new Promise(function(resolve, reject) {
		if (showLoading) {
			//uni.showLoading({
			//	mask: true
			//});
		}
		uni.request({
			url: link,
			method: method.toUpperCase(),
			data: data,
			dataType: 'json',
			header: header,
			success(data) {
				if (data.data.status == '-1') {
					uni.showToast({
						title: data.data.msg,
						duration: 2000,
						icon: 'none'
					});
					// 未登录
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/login/loginAndRegister'
						})
					}, 100)
				} else {
					resolve(data);
				}

				uni.hideLoading()

			},
			fail: function(data) {
				reject(data);
			}
		})
	})
	return promise
}
